package com.newly.center.business.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newly.center.business.service.CheckRecordService;
import com.newly.center.business.service.MediaService;
import com.newly.common.base.entity.business.Dto.CheckRecordDto;
import com.newly.common.base.entity.business.Po.CheckRecord;
import com.newly.common.base.entity.business.Po.Media;
import com.newly.common.mapper.business.MediaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/11/8 15:11
 */
@Service("mediaService")
public class MediaServiceImpl extends ServiceImpl<MediaMapper, Media> implements MediaService {

    @Autowired
    private CheckRecordService checkRecordService;

    @Override
    public List<String> getPictureUrls() {
        List<String> pictureUrls = new ArrayList<>();
        //最近24小时的时间
        String time = DateUtil.offsetHour(DateUtil.date(),-24).toString();
        CheckRecordDto checkRecordDto = new CheckRecordDto().setStartTime(time).setEndTime(DateUtil.now());
        //查询超重的记录
        List<CheckRecord> checkRecordList = checkRecordService.getListByCondition(checkRecordDto);
        if (checkRecordList.isEmpty())
            return pictureUrls;
        List<Integer> ids = checkRecordList.stream().map(CheckRecord::getId).collect(Collectors.toList());
        List<Media> mediaList = this.list(new QueryWrapper<Media>().lambda().in(Media::getOrderId,ids).eq(Media::getType,1).orderByDesc(Media::getId));
        if (ObjectUtil.isEmpty(mediaList))
            return pictureUrls;
        pictureUrls = mediaList.stream().map(Media::getPictureUrl).filter(p -> !StringUtils.isEmpty(p)).collect(Collectors.toList());
        return pictureUrls;
    }
}
